﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using Microsoft.WowAddonStudio.FrameXml.Components.Editors;
using Microsoft.WowAddonStudio.FrameXml.Serialization.Attributes;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Descriptors.Filters
{
    class UiTypeEditorPropertyDescriptorFilter : PropertyDescriptorFilter
    {
        private static readonly Dictionary<Type, Type> uiTypeEditors = InitializeUiTypeEditors();
        private static Dictionary<Type, Type> InitializeUiTypeEditors()
        {
            return new Dictionary<Type, Type>
                       {
                           {typeof (ColorAttribute), typeof (FrameXmlColorEditor)},
                           {typeof (BackdropAttribute), typeof (BackdropEditor)},
                           {typeof (AnchorAttribute), typeof (AnchorCollectionEditor)},
                           {typeof (ShadowAttribute), typeof (ShadowEditor)},
                           {typeof (FileAttribute), typeof(TreePickerEditor)},
                           {typeof (ControlSelectorAttribute), typeof(ControlSelectorEditor)},
                           {typeof (FontStringAttribute), typeof(FontStringEditor)},
                           {typeof (TextureAttribute), typeof (TextureEditor)}
                       };
        }

        /// <summary>
        /// Filters the specified property descriptor.
        /// </summary>
        /// <param name="instance">The instance to filter.</param>
        /// <param name="propertyDescriptor">The property descriptor.</param>
        /// <returns>A property descriptor</returns>
        public override PropertyDescriptor Filter(object instance, PropertyDescriptor propertyDescriptor)
        {
            foreach (var uiTypeEditor in uiTypeEditors)
            {
                if (propertyDescriptor.Attributes.Cast<Attribute>().Any(attribute => attribute.GetType() == uiTypeEditor.Key))
                    return TypeDescriptor.CreateProperty(propertyDescriptor.ComponentType, propertyDescriptor, new EditorAttribute(uiTypeEditor.Value, typeof (UITypeEditor)));
            }

            return propertyDescriptor;
        }
    }
}
